package org.budo.activiti.entity;

import java.util.List;
import java.util.Map;

import org.activiti.engine.impl.TaskQueryImpl;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityManager;
import org.activiti.engine.task.Task;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.budo.support.spring.util.SpringUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public class TaskEntityManagerDelegate implements TaskEntityManager {
    private TaskEntityManager taskEntityManager;

    private TaskEntityManager getTaskEntityManager() {
        if (null != this.taskEntityManager) {
            return this.taskEntityManager;
        }

        SpringProcessEngineConfiguration _configuration = SpringUtil.getBean(SpringProcessEngineConfiguration.class);
        if (null == _configuration) {
            log.error("#30 configuration is null, configuration=" + _configuration + ", this=" + this);
            return null;
        }

        TaskEntityManager _manager = _configuration.getTaskEntityManager();
        if (null == _manager) {
            log.error("#36 taskEntityManager is null, configuration=" + _configuration + ", this=" + this);
            return null;
        }

        return this.taskEntityManager = _manager;
    };

    @Override
    public TaskEntity create() {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().create();
    }

    @Override
    public TaskEntity findById(String entityId) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findById(entityId);
    }

    @Override
    public void insert(TaskEntity entity) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().insert(entity);
    }

    @Override
    public void insert(TaskEntity entity, boolean fireCreateEvent) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().insert(entity, fireCreateEvent);
    }

    @Override
    public TaskEntity update(TaskEntity entity) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().update(entity);
    }

    @Override
    public TaskEntity update(TaskEntity entity, boolean fireUpdateEvent) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().update(entity, fireUpdateEvent);
    }

    @Override
    public void delete(String id) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().delete(id);
    }

    @Override
    public void delete(TaskEntity entity) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().delete(entity);
    }

    @Override
    public void delete(TaskEntity entity, boolean fireDeleteEvent) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().delete(entity, fireDeleteEvent);
    }

    @Override
    public void insert(TaskEntity taskEntity, ExecutionEntity execution) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().insert(taskEntity, execution);
    }

    @Override
    public void changeTaskAssignee(TaskEntity taskEntity, String assignee) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().changeTaskAssignee(taskEntity, assignee);
    }

    @Override
    public void changeTaskOwner(TaskEntity taskEntity, String owner) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().changeTaskOwner(taskEntity, owner);
    }

    @Override
    public List<TaskEntity> findTasksByExecutionId(String executionId) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findTasksByExecutionId(executionId);
    }

    @Override
    public List<TaskEntity> findTasksByProcessInstanceId(String processInstanceId) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findTasksByProcessInstanceId(processInstanceId);
    }

    @Override
    public List<Task> findTasksByQueryCriteria(TaskQueryImpl taskQuery) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findTasksByQueryCriteria(taskQuery);
    }

    @Override
    public List<Task> findTasksAndVariablesByQueryCriteria(TaskQueryImpl taskQuery) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findTasksAndVariablesByQueryCriteria(taskQuery);
    }

    @Override
    public long findTaskCountByQueryCriteria(TaskQueryImpl taskQuery) {
        if (null == this.getTaskEntityManager()) {
            return 0;
        }

        return this.getTaskEntityManager().findTaskCountByQueryCriteria(taskQuery);
    }

    @Override
    public List<Task> findTasksByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findTasksByNativeQuery(parameterMap, firstResult, maxResults);
    }

    @Override
    public long findTaskCountByNativeQuery(Map<String, Object> parameterMap) {
        if (null == this.getTaskEntityManager()) {
            return 0;
        }

        return this.getTaskEntityManager().findTaskCountByNativeQuery(parameterMap);
    }

    @Override
    public List<Task> findTasksByParentTaskId(String parentTaskId) {
        if (null == this.getTaskEntityManager()) {
            return null;
        }

        return this.getTaskEntityManager().findTasksByParentTaskId(parentTaskId);
    }

    @Override
    public void updateTaskTenantIdForDeployment(String deploymentId, String newTenantId) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().updateTaskTenantIdForDeployment(deploymentId, newTenantId);
    }

    @Override
    public void deleteTask(String taskId, String deleteReason, boolean cascade) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().deleteTask(taskId, deleteReason, cascade);
    }

    @Override
    public void deleteTasksByProcessInstanceId(String processInstanceId, String deleteReason, boolean cascade) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().deleteTasksByProcessInstanceId(processInstanceId, deleteReason, cascade);
    }

    @Override
    public void deleteTask(TaskEntity task, String deleteReason, boolean cascade, boolean cancel) {
        if (null == this.getTaskEntityManager()) {
            return;
        }

        this.getTaskEntityManager().deleteTask(task, deleteReason, cascade, cancel);
    }
}